package com.sun.controller.admin;

import com.alibaba.fastjson.JSON;
import com.sun.pojo.Employee;
import com.sun.pojo.Menu;
import com.sun.pojo.MenuNode;
import com.sun.service.MenuService;
import com.sun.utils.DataGridViewResult;
import com.sun.utils.SystemConstant;
import com.sun.utils.TreeNode;
import com.sun.utils.TreeUtil;
import com.sun.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping("/list")
    public String getMenu(HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> home = new HashMap<String, Object>();
        Map<String, Object> logo = new HashMap<String, Object>();

        ArrayList<MenuNode> menuNodes = new ArrayList<MenuNode>();

//        List<Menu> allMenu = menuService.findAllMenu();
        Employee employee = (Employee) session.getAttribute(SystemConstant.LOGINUSER);
        List<Menu> allMenu = menuService.findAllMenuByUserId(employee.getId());
        for (Menu menu : allMenu) {
            MenuNode menuNode = new MenuNode();
            menuNode.setHref(menu.getHref());
            menuNode.setIcon(menu.getIcon());
            menuNode.setPid(menu.getPid());
            menuNode.setId(menu.getId());
            menuNode.setSpread(menu.getSpread());
            menuNode.setTitle(menu.getTitle());
            menuNode.setTarget(menu.getTarget());
            menuNodes.add(menuNode);
        }

        home.put("title", "首页");
        home.put("href", "/admin/desktop.html");//控制器路由,自行定义

        logo.put("title", "酒店管理系统");
        logo.put("image", "/static/layui/images/logo.png");//静态资源文件路径,可使用默认的logo.png
        logo.put("href", "/admin/home.html");//logo点击跳转

        map.put("homeInfo", home);
        map.put("logoInfo", logo);
        map.put("menuInfo", TreeUtil.toTree(menuNodes, 0));

        return JSON.toJSONString(map);


    }

    @RequestMapping("/loadMenuTree")
    public DataGridViewResult loadMenuTree() {
        List<Menu> allMenu = menuService.findAllMenu();
        List<TreeNode> dtreeNode = new ArrayList<TreeNode>();
        for (Menu menu : allMenu) {
            Boolean spread = (menu.getSpread() == null || menu.getSpread() == 1) ? true : false;
            dtreeNode.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), spread));
        }
        return new DataGridViewResult(dtreeNode);
    }

    @RequestMapping("/findMenuByPage")
    public DataGridViewResult findMenuByPage(MenuVo menuVo) {
        return menuService.findMenuByPage(menuVo);
    }

    @RequestMapping("/addMenu")
    public String addMenu(Menu menu) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.addMenu(menu);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "添加成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "添加失败!");
        }

        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateMenu")
    public String updateMenu(Menu menu) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.updateMenu(menu);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "更新成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "更新失败!");
        }

        return JSON.toJSONString(map);
    }

    @RequestMapping("/checkMenuExistChild")
    public String checkMenuExistChild(int id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.checkMenuExistChild(id);
        if (i > 0) {
            map.put(SystemConstant.EXIST, true);
            map.put(SystemConstant.MESSAGE, "该菜单为父级菜单,不能删除!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/deleteMenu")
    public String deleteMenu(int id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = menuService.deleteMenu(id);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败!");
        }
        return JSON.toJSONString(map);
    }

}
